/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sh.ndt.common;

import com.sh.ndt.exam.entity.TestHistory;
import com.sh.ndt.question.entity.Question;
import com.sh.ndt.user.entity.User;
import com.sh.ndt.user.entity.UserDashboardDetail;
import com.sh.ndt.user.entity.UserEntitlement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.apache.log4j.Logger;
//import org.primefaces.json.JSONArray;
//import org.primefaces.json.JSONObject;

/**
 *
 * @author Prashanth
 */
public class WSCaller {

    private static final Logger LOGGER = Logger.getLogger(WSCaller.class);

    public UserEntitlement callAuthenticateUserWS(String urlString, String inputXml) throws Exception {

        return this.callGetUserEntitlentWS(urlString, inputXml);
    }

    /**
     * This method is used to call Signup New User WS.
     *
     * @param urlString
     * @param inputXml
     * @return
     * @throws Exception
     */
    public User callSignupNewUserWS(String urlString, String inputXml) throws Exception {

        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {

            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return ParseJasonObject.getUser(tempStr.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }

    public UserDashboardDetail callAddExamScheduleWS(String urlString, String inputXml) throws Exception {

        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {

            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return ParseJasonObject.getUserDashboard(tempStr.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }

    private UserEntitlement callGetUserEntitlentWS(String urlString, String inputXml) throws Exception {

        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {

            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return ParseJasonObject.getUserEntitlement(tempStr.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }

    public List<Question> callGetQuestionsWS(String urlString, String inputXml) throws Exception {

        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {
            LOGGER.debug("Calling: " + urlString);
            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return ParseJasonObject.getQuestionList(tempStr.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }

    public boolean callSendEmailNotificationWS(String urlString, String inputXml) throws Exception {
        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {
            LOGGER.debug("Calling: " + urlString);
            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return true;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            conn.disconnect();
        }
    }

    public UserEntitlement callEditExamScheduleWS(String urlString, String inputXml) throws Exception {

        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {

            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return ParseJasonObject.getUserEntitlement(tempStr.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }

    public boolean callUpdateMockSessionWS(String urlString, String inputXml) throws Exception {
        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {
            LOGGER.debug("Calling: " + inputXml);
            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return true;

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            conn.disconnect();
        }
    }

    public UserDashboardDetail callGetUserDashboardDetailWS(String urlString, String inputXml) throws Exception {

        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {

            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return ParseJasonObject.getUserDashboard(tempStr.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }

    public List<TestHistory> callGetTestHistoryWS(String urlString, String inputXml) throws Exception {
        URL url;
        HttpURLConnection conn = null;

        OutputStream os;
        BufferedReader br;

        try {
            LOGGER.debug("Calling: " + urlString);
            url = new URL(urlString);

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");

            os = conn.getOutputStream();
            os.write(inputXml.getBytes());
            os.flush();

            if ((conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) || (conn.getResponseCode() == HttpURLConnection.HTTP_OK)) {
                LOGGER.debug("HTTP WS Call Succeded:" + conn.getResponseCode());
            } else {
                throw new Exception("Failed : HTTP error code : " + conn.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;

            StringBuilder tempStr = new StringBuilder();

            LOGGER.debug("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                tempStr.append(output);
            }
            LOGGER.debug("Output from Server .... \n");

            return ParseJasonObject.getTestHistoryList(tempStr.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return null;
    }

}
